/**
 * 
 */
package com.nandasoft.njurep.substance.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nandasoft.njurep.substance.bean.DditeminfoBean;
import com.nandasoft.njurep.substance.bean.WareStateBean;
import com.nandasoft.njurep.substance.bean.WareStoreBean;
import com.nandasoft.njurep.substance.bean.WareStoreDetailBean;
import com.nandasoft.njurep.substance.bean.WareTransBean;
import com.nandasoft.njurep.substance.dao.WareStateDao;
import com.nandasoft.njurep.substance.dao.WareStoreDao;
import com.nandasoft.njurep.substance.dao.WareStoreDetailDao;
import com.nandasoft.njurep.substance.dao.WareTransDao;
import com.nandasoft.njurep.substance.service.CommonService;
import com.nandasoft.njurep.substance.service.WareManagerService;
import com.nandasoft.njurep.substance.utils.Constants;
import com.nandasoft.njurep.substance.utils.UUIDGenerator;

/**
 * @title  事务处理Service
 * @author Administrator
 * @date 2016年9月28日 上午11:35:49
 */
@Service
public class WareManagerServicceImpl extends BaseService implements WareManagerService {

	@Autowired
	private WareStoreDao wareStoreDao;
	@Autowired
	private WareStoreDetailDao wareStoreDetailDao;
	@Autowired
	private WareTransDao wareTransDao;
	@Autowired
	private CommonService commonService;
	@Autowired
	private WareStateDao wareStateDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nandasoft.njurep.substance.service.impl.WareManagerServicce#
	 * updateWareInfo(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void updatePostingWareInfo(String wId, String wType) {
		// TODO Auto-generated method stub
		// 过账(数据状态)
		String state = "1";
		WareStoreBean wsBean = new WareStoreBean();
		wsBean.setId(wId);
		wsBean.setState(state);
		// wsBean.set
		// 1 先更新事务主表及明细表数据状态
		updateWareInfo(wId, state);
		// 2 查询事务配置信息
		operTransWareInfo(wId, wType);
		//
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nandasoft.njurep.substance.service.WareManagerService#
	 * udpateCancelPostingWareInfo(java.lang.String, java.lang.String)
	 */
	@Override
	public void udpateCancelPostingWareInfo(String wId, String wType) {
		// TODO Auto-generated method stub

	}

	/**
	 * 更新事务主表及明细表数据状态
	 * 
	 * @param wId
	 * @param state
	 */
	public void updateWareInfo(String wId, String state) {
		// 事务主表数据
		wareStoreDao.updateWareStoreInfo(wId, state);
		// 事务明细表数据
		wareStoreDetailDao.updateWareStoreDetailInfo(wId, state);
	}

	/**
	 * 根据事务配置执行方法
	 * 
	 * @param wTB
	 */
	public void operTransWareInfo(String wId, String wType) {
		WareTransBean wTB = wareTransDao.getWareTransInfo(wType);
		if (Constants.TYPE_QCSW.equals(wType) && "1".equals(wTB.getBalance())) { // 影响期初事务
			List<WareStoreDetailBean> wsdList = wareStoreDetailDao.getWSDList(wId);
			if (null != wsdList && !wsdList.isEmpty()) {
				WareStateBean wStateBean = new WareStateBean();
				int qcNum = 0;
				double qcMoney = 0;
				double qcMoney1 = 0;
				for (WareStoreDetailBean wareStoreDetailBean : wsdList) {
					qcNum += wareStoreDetailBean.getOrinum();
					qcMoney += wareStoreDetailBean.getOrisum0();
					qcMoney1 += wareStoreDetailBean.getOrisum1();
				}
				wStateBean.setId(UUIDGenerator.getUUID());
				wStateBean.setOriNum(qcNum);
				wStateBean.setOriSum0(qcMoney);
				wStateBean.setOriSum1(qcMoney1);
				wStateBean.setWareCode(wsdList.get(0).getWarecode());
				wStateBean.setStaCode(wsdList.get(0).getStationcode());
				wareStateDao.insertWareStateInfo(wStateBean);
			}
		} else if (Constants.TYPE_CGSW.equals(wType)) { // 采购事务
			// 1 检查采购订单
			updateStockOrders(wTB);
			// 2 影响采购成本

		}

	}

	/**
	 * 计算出库成本
	 * 
	 * @return
	 */
	private Double calculateOutCost() {
		return null;
	}

	/**
	 * 计算入库成本
	 * 
	 * @return
	 */
	private Double calculateInCost() {
		return null;
	}

	/**
	 * 获取系统设置核算方式
	 * 
	 * @return
	 */
	public String getAccountingMethod() {
		DditeminfoBean dBean = commonService.getDdminfoByItemCode("accounting");
		return dBean.getItemCode();
	}

	/**
	 * 更新采购订单数据
	 */
	public void updateStockOrders(WareTransBean wTB) {

	}

	/**
	 * @return the wareStoreDao
	 */
	public WareStoreDao getWareStoreDao() {
		return wareStoreDao;
	}

	/**
	 * @param wareStoreDao
	 *            the wareStoreDao to set
	 */
	public void setWareStoreDao(WareStoreDao wareStoreDao) {
		this.wareStoreDao = wareStoreDao;
	}

	/**
	 * @return the wareStoreDetailDao
	 */
	public WareStoreDetailDao getWareStoreDetailDao() {
		return wareStoreDetailDao;
	}

	/**
	 * @param wareStoreDetailDao
	 *            the wareStoreDetailDao to set
	 */
	public void setWareStoreDetailDao(WareStoreDetailDao wareStoreDetailDao) {
		this.wareStoreDetailDao = wareStoreDetailDao;
	}

	/**
	 * @return the wareTransDao
	 */
	public WareTransDao getWareTransDao() {
		return wareTransDao;
	}

	/**
	 * @param wareTransDao
	 *            the wareTransDao to set
	 */
	public void setWareTransDao(WareTransDao wareTransDao) {
		this.wareTransDao = wareTransDao;
	}

	/**
	 * @return the commonService
	 */
	public CommonService getCommonService() {
		return commonService;
	}

	/**
	 * @param commonService
	 *            the commonService to set
	 */
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	/**
	 * @return the wareStateDao
	 */
	public WareStateDao getWareStateDao() {
		return wareStateDao;
	}

	/**
	 * @param wareStateDao
	 *            the wareStateDao to set
	 */
	public void setWareStateDao(WareStateDao wareStateDao) {
		this.wareStateDao = wareStateDao;
	}
}
